package com.ayg.service;

import com.ayg.mapper.PhotoMapper;
import com.ayg.pojo.Photo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PhotoServiceImpl implements PhotoService{

    @Autowired
    private PhotoMapper photoMapper;

    @Value("${file.localDir}")
    private String localDir;    //获取磁盘路径
    @Value("${file.urlPath}")   //spel表达式
    private String urlPath;     //获取虚拟路径

    /**
     * 查询数据库里面创建好的相册
     * @return
     */
    @Override
    public List<Photo> getPhotoList() {
        //查询数据库的所以数据
        QueryWrapper<Photo> photoListQueryWrapper = new QueryWrapper<>();
        photoListQueryWrapper.eq("parent_id",0);
        List<Photo> list = photoMapper.selectList(photoListQueryWrapper);
        //遍历查询到数据库的list集合
//        for (Photo photo : list) {
//
//        }
        return list;
    }

    /**
     * 创建相册
     * @param photo
     * @return
     */
    @Override
    public Integer savePhoto(Photo photo) {
        QueryWrapper<Photo> queryWrapper = new QueryWrapper<>();
        Integer selectCount = photoMapper.selectCount(queryWrapper);
        System.out.println(selectCount);
        photo.setParentId(0);//创建相册默认ParentId=0
        photo.setStatus(true);//状态默认为true(0)
        photo.setPhotoUrl("123");//设置相册默认背景路径
        photo.setDate(new SimpleDateFormat("/yyyy/MM/dd").format(new Date()));
        photoMapper.insert(photo);
        return selectCount;
    }

    /**
     * 上传图片
     * @param photo
     */
    @Override
    @Transactional
    public void imageUpload(Photo photo) {
        QueryWrapper<Photo> imageUploadQueryWrapper = new QueryWrapper<>();
        imageUploadQueryWrapper.eq("photo_name",photo.getPhotoName()).eq("parent_id",0);
        Photo photoImageUpload = photoMapper.selectOne(imageUploadQueryWrapper);
        System.out.println(photoImageUpload);
        photo.setParentId(1);
        photo.setStatus(true);
        photo.setPhotoPath(photo.getPhotoPath());
        photo.setPhotoName(photoImageUpload.getPhotoName());
        photo.setPhotoTheme(photoImageUpload.getPhotoTheme());
        photo.setPhotoSort(photoImageUpload.getPhotoSort());
        photo.setPhotoDescribe(photoImageUpload.getPhotoDescribe());
        photo.setPhotoUrl(photo.getPhotoUrl());
        photo.setDate(new SimpleDateFormat("/yyyy/MM/dd").format(new Date()));
        photoMapper.insert(photo);

    }

    /**
     * 查询数据库里面上传的图片
     * @return
     */
    @Override
    public List<Photo> getBrowsePhotoList(String photoName) {
        QueryWrapper<Photo> BrowseQueryWrapper = new QueryWrapper<>();
        BrowseQueryWrapper.eq("parent_id",1).eq("photo_name",photoName);
        List<Photo> BrowseList = photoMapper.selectList(BrowseQueryWrapper);

        return BrowseList;
    }

    @Override
    public void photoUpload(Photo photo) {
        String url = photo.getPhotoUrl();
        QueryWrapper<Photo> BrowseQueryWrapper = new QueryWrapper<>();
        BrowseQueryWrapper.eq("photo_name",photo.getPhotoName()).eq("parent_id",0);
        Photo photo1 = photoMapper.selectOne(BrowseQueryWrapper);
        String oldPhotoUrl = photo1.getPhotoUrl();

        //修改图片的路径
        Photo photo2 = photo1.setPhotoUrl(url);
        UpdateWrapper<Photo> photoUpdateWrapper = new UpdateWrapper<>();
        photoUpdateWrapper.eq("photo_url",oldPhotoUrl);
        photoMapper.update(photo2,photoUpdateWrapper);


    }


}
